package com.niguang.schedule;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.niguang.work.api.entity.ReformApply;
import com.niguang.work.api.entity.ReformContent;
import com.niguang.work.api.service.ReformApplyService;
import com.niguang.work.api.service.ReformContentService;
import com.niguang.work.api.weChatTemplate.service.ZhengGaiWeChatService;
import com.niguang.work.api.weChatTemplate.vo.ZhengGaiPersonVo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 整改/复查人审核未操作工单逾期提醒通知
 * 每天 9点 15点 推送
 */

@JobHandler("ScheduleExpire")
@Component
@Slf4j
public class ScheduleExpire extends IJobHandler {

    @Resource
    private ReformApplyService reformApplyService;

    @Resource
    private ReformContentService reformContentService;

    @Resource
    private ZhengGaiWeChatService zhengGaiWeChatService;


    @Override
    public ReturnT<String> execute(String param) throws Exception {
        EntityWrapper<ReformApply> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq(ReformApply.DEAL_FLAG, 0)
                .and("DATEDIFF(reform_term, DATE( NOW( ) ) )  <= 1");
        List<ReformApply> reformApplyList = reformApplyService.selectList(entityWrapper);
        log.info("逾期需要整改/复查工单信息：{}", reformApplyList);
        if (CollectionUtils.isNotEmpty(reformApplyList)) {
            reformApplyList.forEach(p -> {
                ReformContent reformContent = reformContentService.selectById(p.getContentId());
                ZhengGaiPersonVo zhengGaiPersonVo = new ZhengGaiPersonVo(p.getApplyOpenId(), String.valueOf(p.getContentId()),
                        reformContent.getDangerTitle(), p.getReformTerm());
                zhengGaiWeChatService.sendExpire(zhengGaiPersonVo);
            });
        } else {
            log.info("无逾期整改/复查人审核工单");
        }
        return ReturnT.SUCCESS;
    }
}
